define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wechat/fans/index' + location.search,
                    add_url: 'wechat/fans/add',
                    edit_url: 'wechat/fans/edit',
                    del_url: 'wechat/fans/del',
                    multi_url: 'wechat/fans/multi',
                    import_url: 'wechat/fans/import',
                    table: 'wechat_fans',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'third.user.nickname', title: __('Associated Users'), operate: false},
                        {field: 'openid', title: __('Openid'), operate: 'LIKE'},
                        {field: 'is_subscribe', title: __('Is_subscribe'), searchList: {"0":__('Is_subscribe 0'),"1":__('Is_subscribe 1')}, formatter: Table.api.formatter.status},
                        {field: 'subscribe_time', title: __('Subscribe_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            var page_count = 0;
            var page_index = 0;
            $(document).on('click','.btn-sync',function () {
                Layer.confirm(__('Are you sure to synchronize fan information?'), {
                    icon: 3,
                    title: __('Warning')
                }, function (index) {
                    window.refresh(index);
                });
            });

            window.refresh = function(index){
                Backend.api.ajax({
                    url: "wechat/fans/sync",
                    data : {
                        "page" : page_index,
                    },
                }, function (data,res) {
                    if(data.page_count){
                        page_count = data.page_count;
                    }

                    if (page_index < page_count) {
                        page_index = parseInt(page_index) + 1;
                        window.refresh(index);
                    }else{
                        Toastr.success(__('Sync successful'));
                        Layer.close(index);
                        $("#table").bootstrapTable('refresh',{});//刷新表格
                    }
                    return false;
                });
            };
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
